<?php

/*
	PIE Content Management System
	John Girata
	Licensed under the GNU General Public License version 3
*/

defined('PIESECVAR') or die('Restricted Access');

class Controller
{
	private $content = '';
	private $title = '';
	
	function add_content($text)
	{
		$this->content .= $text;
	}
	
	function set_title($text)
	{
		$this->title = $text;
	}
	
	function print_page()
	{
		global $params, $module;
		
		// Load the template file
		$tmpl = file_get_contents(TMPL_DIR . 'main.tmpl');
		
		// Plug in the title and content variables
		$patterns = Array(
			'/<%%TITLE%%>/',
			'/<%%CONTENT%%>/',
		);
		
		$replacements = Array(
			$params->site_name . ' - ' . $this->title,
			$this->content
		);
		
		$html = preg_replace($patterns, $replacements, $tmpl);
		
		// Check for and load any modules
		$module->parse($html);
		
		echo $html;
	}
	
	function add_log($log, $msg)
	{
		$fopen = fopen(LOG_DIR . $log . '.log', 'a+');
		fwrite($fopen, $msg);
	}
		
	function log_error($msg, $report = false)
	{
		global $params;
		
		$this->add_log("errors", "$msg\n");
		
		if($report)
			$this->send_mail($params->webmaster_email, $params->site_name . ' - Error', $msg);
	}
	
	function send_mail($to, $subject, $message)
	{
		$this->add_log('mail', "$to\t$subject\t" . addslashes($message));
		
		echo mail($to, $subject, $message);
	}
}

?>